package com.mmall.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.mmall.common.ServerResponse;
import com.mmall.dto.AclModuleDto;
import com.mmall.param.RoleParam;
import com.mmall.pojo.SysRole;
import com.mmall.pojo.SysUser;
import com.mmall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author wangtengyu
 * @Create 2018-03-15-13:38
 */
@Controller
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    private ISysRoleService iSysRoleService;

    @Autowired
    private ISysTreeService iSysTreeService;

    @Autowired
    private ISysRoleAclService iSysRoleAclService;

    @Autowired
    private ISysRoleUserService iSysRoleUserService;

    @Autowired
    private ISysUserService iSysUserService;


    @RequestMapping("/role.page")
    public ModelAndView page() {
        return new ModelAndView("role");
    }

    /**
     * 保存
     *
     * @param param
     * @return
     */
    @RequestMapping("/save.json")
    @ResponseBody
    public ServerResponse saveRole(RoleParam param) {
        iSysRoleService.save(param);
        return ServerResponse.createBySuccess("新增角色成功");
    }

    /**
     * 更新
     *
     * @param param
     * @return
     */
    @RequestMapping("/update.json")
    @ResponseBody
    public ServerResponse updateRole(RoleParam param) {
        iSysRoleService.update(param);
        return ServerResponse.createBySuccess("更新角色成功");
    }

    /**
     * 角色列表
     *
     * @param param
     * @return
     */
    @RequestMapping("/list.json")
    @ResponseBody
    public ServerResponse list(RoleParam param) {
        List<SysRole> roleList = iSysRoleService.getAll();
        return ServerResponse.createBySuccess(roleList);
    }

    /**
     * 删除
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/delete.json")
    @ResponseBody
    public ServerResponse delete(@RequestParam("id") int roleId) {
        iSysRoleService.delete(roleId);
        return ServerResponse.createBySuccess("删除角色成功");
    }

    /**
     * 角色对应的权限树
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/roleAclTree.json")
    @ResponseBody
    public ServerResponse roleAclTree(@RequestParam("roleId") int roleId) {
        List<AclModuleDto> aclModuleDtoList = iSysTreeService.roleAclTree(roleId);
        return ServerResponse.createBySuccess(aclModuleDtoList);

    }

    /**
     * 变更角色的权限
     *
     * @param roleId
     * @param aclIds
     * @return
     */
    @RequestMapping("changeAcls.json")
    @ResponseBody
    public ServerResponse changeAcls(@RequestParam("roleId") int roleId, @RequestParam(value = "aclIds", required = false, defaultValue = "") String aclIds) {
        //List<Integer> aclIdList= StringUtil.splitToListInt(aclIds);
        //另一种做法
        List<Integer> aclIdList = Lists.newArrayList();
        String[] strArray = aclIds.split(",");
        for (int i = 0; i < strArray.length; i++) {
            if (strArray[i].trim().length() > 0) {
                aclIdList.add(Integer.valueOf(strArray[i]));
            }

        }
        iSysRoleAclService.changeAcls(roleId, aclIdList);
        return ServerResponse.createBySuccess("更改角色权限成功");
    }


    @RequestMapping("changeUsers.json")
    @ResponseBody
    public ServerResponse changeUsers(@RequestParam("roleId") int roleId,@RequestParam(value = "userIds",required = false,defaultValue = "")String userIds){

        List<Integer> userIdList= Lists.newArrayList();
        String[] strArray= userIds.split(",");
        for(int i=0;i<strArray.length;i++){
            if(strArray[i].trim().length()>0){
                userIdList.add(Integer.valueOf(strArray[i]));
            }
        }
        iSysRoleUserService.changeUsers(roleId,userIdList);
        return ServerResponse.createBySuccess("更改用户角色成功");
    }


    @RequestMapping("users.json")
    @ResponseBody
    public ServerResponse getUsers(@RequestParam("roleId") int roleId) {

        List<SysUser> selectedUserList = iSysRoleUserService.getUserListByRoleId(roleId);
        List<SysUser> allUserList = iSysUserService.getAll();
        List<SysUser> unSelectedUserList = Lists.newArrayList();

        Set<Integer> selectedUserIdSet = Sets.newHashSet();
        for (SysUser sysUser : selectedUserList) {
            selectedUserIdSet.add(sysUser.getId());
        }

        for (SysUser sysUser : allUserList) {
            if (sysUser.getStatus() == 1 && !selectedUserIdSet.contains(sysUser.getId())){
                unSelectedUserList.add(sysUser);
            }
        }

        Map<String,List<SysUser>> map= Maps.newHashMap();
        map.put("selected",selectedUserList);
        map.put("unselected",unSelectedUserList);
        return ServerResponse.createBySuccess(map);


    }


}
